package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.AClassEntity;
import com.company.project.service.AClassService;

import javax.validation.Valid;


/**
 * ${comments}
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2022-06-05 22:49:34
 */
@Controller
@RequestMapping("/")
public class AClassController {
    @Autowired
    private AClassService aClassService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/aClass")
    public String aClass() {
        return "aclass/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("aClass/add")
    @RequiresPermissions("aClass:add")
    @ResponseBody
    public DataResult add(@RequestBody @Valid AClassEntity aClass){
        aClassService.save(aClass);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("aClass/delete")
    @RequiresPermissions("aClass:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        aClassService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("aClass/update")
    @RequiresPermissions("aClass:update")
    @ResponseBody
    public DataResult update(@RequestBody @Valid AClassEntity aClass){
        if(null==aClass.getId()){
            throw new BusinessException("班级ID不存在");
        }
        aClassService.updateById(aClass);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("aClass/listByPage")
    @RequiresPermissions("aClass:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody AClassEntity aClass){
        Page page = new Page(aClass.getPage(), aClass.getLimit());
        LambdaQueryWrapper<AClassEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.like(StringUtils.isNotBlank(aClass.getName()),AClassEntity::getName, aClass.getName());
        IPage<AClassEntity> iPage = aClassService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
